package com.jinlejia.erp.ui.house.prospect;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ActivityProspectReplenishBinding;
import com.jinlejia.erp.ui.house.prospect.bean.SurveyPictureBean;

/**
 * 补充房源信息
 */
public class ProspectReplenishActivity extends AppCompatActivity {
    private Helper helper;

    private String houseSourceCode;
    private String houseSourceType;
    private String purpose;

    private boolean isReason;
    private ActivityProspectReplenishBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_prospect_replenish);
        binding.updateHouseLayout.setOnClickListener(v->updateHouseLayout());
        binding.addHouseTypeLayout.setOnClickListener(v -> addHouseTypeLayout());
        binding.toolbarBackImageView.setOnClickListener(v -> toolBarFinish());

        helper = Helper.init(this);
        Intent intent = getIntent();
        houseSourceCode = intent.getStringExtra(Type.PARAM.HOUSE_SOURCE_CODE);
        houseSourceType = intent.getStringExtra(Type.PARAM.HOUSE_SOURCE_TYPE);
        purpose = intent.getStringExtra(Type.PARAM.HOUSE_USAGE_NAME);
        houseType = intent.getStringExtra(Type.PARAM.HOUSE_TYPE);
        isReason = intent.getBooleanExtra(Type.PARAM.REASON,false);
        binding.toolbarTitleTextView.setText("实勘图片上传");
        init();
    }
    private SurveyPictureBean mBean;
    private String houseType;
    private void init() {
        helper.http().getServey(houseSourceType, houseSourceCode, bean -> {
            if (bean!=null) {
                mBean = bean.toBean();
                houseType = helper.util().formatHouseInfo(bean.getRoom(),bean.getHall(),bean.getToilet(),bean.getKitchen()).toString();
                purpose = bean.getPurpose();
                houseType = bean.getRoom()+"-"+bean.getHall()+"-"+bean.getKitchen()+"-"+bean.getToilet();
            }
        });
    }

    public void updateHouseLayout(){
        Intent intent = createIntent();
        if (intent==null)return;
        intent.setClass(this,ProspectIndexActivity.class);
        intent.putExtra(Type.PARAM.IMAGE_SHOW_TYPE,0);
        startActivityForResult(intent,20);
    }

    public void addHouseTypeLayout(){
        Intent intent = createIntent();
        if (intent==null)return;
        intent.putExtra(Type.PARAM.IMAGE_SHOW_TYPE,2);
        startActivityForResult(intent,20);
    }

    private Intent createIntent(){
        if (TextUtils.isEmpty(purpose)){
            return null;
        }
        Intent temp = getIntent();
        Intent intent = new Intent();
        intent.setClass(this,ProspectUploadImageActivity.class);

        int usageType = helper.util().mapUsageNameToUsageType(purpose);
        intent.putExtra(Type.PARAM.HOUSE_USAGE_TYPE,usageType); //房源用途
        intent.putExtra(Type.PARAM.HOUSE_USAGE_NAME,this.purpose);   //房屋用途
        intent.putExtra(Type.PARAM.PAGE_TYPE,3);    //补充房源信息
        intent.putExtra(Type.PARAM.HOUSE_USAGE,purpose); //房屋用途String，供ProspectIndexActivity使用
        intent.putExtra(Type.PARAM.HOUSE_SOURCE_CODE,houseSourceCode); //房源编码
        intent.putExtra(Type.PARAM.HOUSE_SOURCE_TYPE,houseSourceType);//房源类型
        intent.putExtra(Type.PARAM.REASON,isReason); //是否需要顶替理由
        //户型信息 格式为3-2-1-0 含义为：3室2厅1卫0台
        if (TextUtils.isEmpty(houseType)){
            intent.putExtra(Type.PARAM.HOUSE_TYPE,temp.getStringExtra(Type.PARAM.HOUSE_TYPE));
        }else {
            intent.putExtra(Type.PARAM.HOUSE_TYPE,houseType);
        }
        return intent;
    }

    public void toolBarFinish(){
        this.finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode== Activity.RESULT_OK){
            if (requestCode==20){
                setResult(Activity.RESULT_OK);
                finish();
            }
        }
    }
}
